<?php

namespace App\Providers;

use App\Models\AssetAffiliation;
use App\Models\AssetBrand;
use App\Models\AssetCategory;
use App\Models\AssetKeeper;
use App\Models\AssetLocation;
use App\Models\AssetStatus;
use App\Models\AssetVendor;
use App\Observers\Asset\AssetPivotObserver;
use Hyn\Tenancy\Environment;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        if ($this->app->environment() !== 'production') {
            $this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);
        }
        \Laravel\Passport\Passport::ignoreMigrations();
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        $env = app(Environment::class);

        if ($fqdn = optional($env->hostname())->fqdn) {
            config(['database.default' => 'tenant']);
        }
        AssetAffiliation::observe(AssetPivotObserver::class);
        AssetBrand::observe(AssetPivotObserver::class);
        AssetKeeper::observe(AssetPivotObserver::class);
        AssetVendor::observe(AssetPivotObserver::class);
        AssetLocation::observe(AssetPivotObserver::class);
        AssetStatus::observe(AssetPivotObserver::class);
        AssetCategory::observe(AssetPivotObserver::class);
        if (env('APP_DEBUG')) {
            DB::listen(function ($query) {
                File::append(
                    storage_path('/logs/query.log'),
                    $query->sql . PHP_EOL
                );
            });
        }
    }
}
